<?php

namespace Console\Logic;

use \Common\Logic\BaseLogic;

class AdLogic extends BaseLogic
{
    public static $typeDic = array(
        "1" => "首页轮播",
        "2" => "任务赚轮播"
    );

    private $adModel;

    public function __construct()
    {
        parent::__construct();
        $this->adModel = D('Ad');
    }

    /**
     * 返回类型字典
     * @return array
     */
    public function getTypeDic()
    {
        return static::$typeDic;
    }

    /**
     * 根据条件统计
     * @param array $where
     * @return mixed
     */
    public function statistics($where = array())
    {
        return $this->adModel->where($where)->count();
    }

    /**
     * 查找一条广告
     *
     * @param [type] $where
     * @return void
     */
    public function find($where)
    {
        return $this->adModel->where($where)->find();
    }

    /**
     * 获取广告列表
     * @param int $page
     * @param int $size
     * @param $where
     * @return mixed
     */
    public function getList($page = 1, $size = 20, $where)
    {
        $list = $this->adModel->where($where)->page($page, $size)->order("id desc")->select();
        $total = $this->statistics($where);
        return static::$res->suc("成功", array(
            "list" => $list,
            "total" => $total
        ));
    }

    /**
     * 添加广告
     *
     * @param [type] $data
     * @return void
     */
    public function add($data)
    {
        if (empty($data)) {
            return static::$res->err("参数异常");
        }
        $id = $this->adModel->add($data);
        if (empty($id)) {
            return static::$res->err("数据库异常,保存失败");
        }
        return static::$res->suc("成功", $id);
    }

    /**
     * 编辑广告
     *
     * @param [type] $where
     * @param [type] $data
     * @return void
     */
    public function save($where,$data)
    {
        if (empty($data) || empty($where)) {
            return static::$res->err("参数异常");
        }

        $count = $this->adModel->where($where)->save($data);
        if (empty($count)) {
            return static::$res->err("数据库异常编辑失败");
        }
        return static::$res->suc("成功");
    }
}
